﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Virvar.TCPServer
{
    class Program
    {
        static TcpListener tcpListener;

        static void Main(string[] args)
        {
            int PORT = 11000;
            IPAddress ipAddr = IPAddress.Parse("127.0.0.1");
            tcpListener = new TcpListener(ipAddr, PORT);
            tcpListener.Start(10);
            Console.WriteLine("Waiting for connections...");
            Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
            while (true)
            {
                TcpClient tcpClient = tcpListener.AcceptTcpClient();
                Console.WriteLine("Someone trying to connect...");
                ClientHandler clientHandler = new ClientHandler(tcpClient);
                Thread clientThrd = new Thread(new ThreadStart(clientHandler.Run));
                clientThrd.Start();
            }
        }

        static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            tcpListener.Stop();
            ClientHandler.DisconnectAll();
        }
    }
}
